Scroll Progress Bar

Operators

In the C programming language, operators are symbols that perform specific operations on one or more operands. C provides a variety of operators, each with its specific functionality. Let's go through some of the essential operators in C along with examples:
Arithmetic Operators:

Arithmetic operators are used for basic arithmetic calculations.


#include <stdio.h>

int main() {
    int a = 10, b = 5;

    int sum = a + b;
    int difference = a - b;
    int product = a * b;
    int quotient = a / b;
    int remainder = a % b;

    printf("Sum: %d\n", sum);               // Output: 15
    printf("Difference: %d\n", difference); // Output: 5
    printf("Product: %d\n", product);       // Output: 50
    printf("Quotient: %d\n", quotient);     // Output: 2
    printf("Remainder: %d\n", remainder);   // Output: 0

    return 0;
}
Relational Operators:

Relational operators compare two values and return a boolean (1 for true, 0 for false).


#include <stdio.h>

int main() {
    int a = 10, b = 5;

    int result1 = a > b;
    int result2 = a < b;
    int result3 = a >= b;
    int result4 = a <= b;
    int result5 = a == b;
    int result6 = a != b;

    printf("Result1: %d\n", result1); // Output: 1 (True)
    printf("Result2: %d\n", result2); // Output: 0 (False)
    printf("Result3: %d\n", result3); // Output: 1 (True)
    printf("Result4: %d\n", result4); // Output: 0 (False)
    printf("Result5: %d\n", result5); // Output: 0 (False)
    printf("Result6: %d\n", result6); // Output: 1 (True)

    return 0;
}
Logical Operators:

Logical operators are used to perform logical operations and return a Boolean value.


#include <stdio.h>

int main() {
    int x = 1, y = 0;

    int result1 = x && y; // Logical AND
    int result2 = x || y; // Logical OR
    int result3 = !x;     // Logical NOT

    printf("Result1: %d\n", result1); // Output: 0 (False)
    printf("Result2: %d\n", result2); // Output: 1 (True)
    printf("Result3: %d\n", result3); // Output: 0 (False)

    return 0;
}
Assignment Operators:

Assignment operators are used to assign values to variables.


#include <stdio.h>

int main() {
    int a = 10;

    a += 5; // Equivalent to: a = a + 5;
    printf("a: %d\n", a); // Output: 15

    a -= 3; // Equivalent to: a = a - 3;
    printf("a: %d\n", a); // Output: 12

    a *= 2; // Equivalent to: a = a * 2;
    printf("a: %d\n", a); // Output: 24

    a /= 4; // Equivalent to: a = a / 4;
    printf("a: %d\n", a); // Output: 6

    return 0;
}
Increment and Decrement Operators:

Increment and decrement operators are used to increase or decrease the value of a variable by 1.


#include <stdio.h>

int main() {
    int a = 5;

    printf("a: %d\n", a); // Output: 5

    a++; // Increment
    printf("a: %d\n", a); // Output: 6

    a--; // Decrement
    printf("a: %d\n", a); // Output: 5

    return 0;
}
Conditional operator:

In C language, the conditional operator, also known as the ternary operator, allows to write a compact if-else statement in a single line. It has the following syntax:

condition ? expression1 : expression2;

If the condition is true, expression1 is evaluated and becomes the value of thpe whole expression. If the condition is false, expression2 is evaluated and becomes the value of the whole expression.

Here's an example to demonstrate the use of the conditional operator:

#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);

    // Using the conditional operator to check if the number is positive or negative
    const char* result = num >= 0 ? "Positive" : "Negative";

    printf("The number is %s\n", result);

    return 0;
}

In this example, the program takes input from the user and stores it in the variable num. The conditional operator is then used to determine if the number is positive or negative. If num >= 0 is true, the expression "Positive" is assigned to the variable result, otherwise, the expression "Negative" is assigned. The value of result is then printed to the console.

These are some of the most commonly used operators in C. There are also other operators like bitwise operators, conditional operator (ternary operator), etc. that are used in more specific scenarios.


What is the operator used for addition in C?


Addition

What operator is used for subtraction in C?


Subtraction

Which operator is used for multiplication in C?


Multiplication

What operator is used for division in C?


Division

What operator is used to compare two values for equality in C?


Equality